﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
/// Summary description for YQLJ
/// </summary>
public partial class YQLJ
{
    public static YQLJ Get(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.YQLJ.FirstOrDefault(row => row.ID == id);
        }
    }

    public static List<YQLJ> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.YQLJ.OrderByDescending(row => row.Rank).ToList();
        }
    }

    public static List<YQLJ> GetAllByConditon(string Key)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from yqlj in context.YQLJ
                        select yqlj;
            if (!string.IsNullOrEmpty(Key))
                query = query.Where(row => row.Title.Contains(Key) || row.Link.Contains(Key));
            return query.OrderByDescending(row => row.Rank).ToList();
        }
    }
  

    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.YQLJ.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<YQLJ>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.YQLJ.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.YQLJ.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }
}